/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git.http;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import org.zhiqim.git.GitConstants;


/**
 * 边频带进度
 *
 * @version v1.0.0 @author zouzhigang 2017-5-12 新建与整理
 */
class ProgressSideBand implements Progress, GitConstants
{
    private final PrintWriter out;
    
    private boolean output;
    private long taskBeganAt;
    private long lastOutput;
    private String msg;
    private int lastWorked;
    private int totalWork;

    ProgressSideBand(OutputStream os)
    {
        this.out = new PrintWriter(new OutputStreamWriter(os, _UTF_8_C_));
    }
    
    public void start()
    {
        taskBeganAt = System.currentTimeMillis();
        lastOutput = taskBeganAt;
    }

    public void beginTask(final String title, final int total)
    {
        endTask();
        msg = title;
        lastWorked = 0;
        totalWork = total;
    }

    public void update(final int completed)
    {
        if (msg == null)
            return;

        final int cmp = lastWorked + completed;
        final long now = System.currentTimeMillis();
        if (!output && now - taskBeganAt < 500)
            return;
        
        if (totalWork == UNKNOWN)
        {
            if (now - lastOutput >= 500)
            {
                display(cmp, null);
                lastOutput = now;
            }
        }
        else
        {
            if ((cmp * 100 / totalWork) != (lastWorked * 100) / totalWork || now - lastOutput >= 500)
            {
                display(cmp, null);
                lastOutput = now;
            }
        }
        
        lastWorked = cmp;
        output = true;
    }

    public void endTask()
    {
        if (output)
        {
            if (totalWork == UNKNOWN)
                display(lastWorked, ", done\n");
            else
                display(totalWork, "\n");
        }
        
        output = false;
        msg = null;
    }
    
    private void display(final int cmp, final String eol)
    {
        StringBuilder strb = new StringBuilder();
        strb.append(msg);
        strb.append(": ");

        if (totalWork == UNKNOWN)
        {
            strb.append(cmp);
        }
        else
        {
            final int pcnt = (cmp * 100 / totalWork);
            if (pcnt < 100)
                strb.append(' ');
            if (pcnt < 10)
                strb.append(' ');
            strb.append(pcnt);
            strb.append("% (");
            strb.append(cmp);
            strb.append("/");
            strb.append(totalWork);
            strb.append(")");
        }
        if (eol != null)
            strb.append(eol);
        else
            strb.append("   \r");
        
        out.print(strb);
        out.flush();
    }
}
